package com.noahsloan.groovy.algorithms

/**
 * @author noah
 *
 */
public class Graph implements Writable {

	def name
	def nodes
	
	Writer writeTo(Writer out) {
		writeDot(out,nodes.values()*.outgoing.flatten(),name)
		out
	}
	
	static void writeDot(Writer out,edges,name = null) {
		out.write "strict digraph ${name?:''} {\n"
		edges.each { it ->
			out.write "  ${it};\n"
		}
		out.write "}\n"
	}

	String toString() {
		(new StringWriter() << this).toString()
	}
	
	int size() { nodes.size() }
}
